package com.kefu.web.im.config.web;

import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.kefu.web.im.config.web.handle.KeFuExceptionHandler;
import com.kefu.web.im.netty.event.UserDataEvent;
import com.kefu.web.im.netty.event.handler.UserDataEventFactory;
import com.kefu.web.im.netty.event.handler.UserEventHandler;
import com.kefu.web.im.netty.event.handler.multiupdate.MultiUpdateEventFactory;
import com.kefu.web.im.netty.event.handler.multiupdate.MultiUpdateEventHandler;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

@Component
public class DisruptorConfigure {
	/**
	 * 破坏者
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Bean(name = "disruptor")
	public Disruptor<UserDataEvent> disruptor() {
//		Executor executor = Executors.newCachedThreadPool();
		UserDataEventFactory factory = new UserDataEventFactory();
		Disruptor<UserDataEvent> disruptor = new Disruptor<UserDataEvent>(factory, 1024,
				Executors.defaultThreadFactory(), ProducerType.SINGLE, new YieldingWaitStrategy());
		disruptor.setDefaultExceptionHandler(new KeFuExceptionHandler());
		disruptor.handleEventsWith(new UserEventHandler());
		disruptor.start();
		return disruptor;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Bean(name = "multiupdate")
	public Disruptor<UserDataEvent> multiupdate() {
//		Executor executor = Executors.newCachedThreadPool();
		MultiUpdateEventFactory factory = new MultiUpdateEventFactory();
		Disruptor<UserDataEvent> disruptor = new Disruptor<UserDataEvent>(factory, 1024, Executors.defaultThreadFactory(), ProducerType.SINGLE,
				new YieldingWaitStrategy());
		disruptor.handleEventsWith(new MultiUpdateEventHandler());
		disruptor.setDefaultExceptionHandler(new KeFuExceptionHandler());
		disruptor.start();
		return disruptor;
	}
}
